var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var OpenBrowserPlugin = require('open-browser-webpack-plugin');

module.exports = {
	devtool: 'cheap-module-eval-source-map',
	entry: {
		index: './websrc/main/index.js'
		//, 'some-other-page': ['./websrc/main/some/other/page.js']
	},
	output: {
		path: __dirname + '/src/main/webapp/assets/js',
		filename: '[name].js'
	},

	module: {
		rules: [
			{
				test: /\.js$/,
				include: [
					path.resolve(__dirname, "websrc/main"),
					path.resolve(__dirname, "websrc/test")
				],
				use: [{
					loader: 'babel-loader',
					query: {
						presets: ['es2015', 'stage-0', 'react'],
						// Ant Design 设置按需加载
						'plugins': [
							['import', {'libraryName': 'antd', 'style': 'css'}]// `style: true` 会加载 less 文件
						]
					}
				}]
			}, {
				test: /\.css$/,
				include: [
					path.resolve(__dirname, "websrc/main"),
					path.resolve(__dirname, "websrc/test")
				],
				use: [{
					loader: 'style-loader'
				}, {
					loader: 'css-loader',
					options: {
						importLoaders: 1,
						modules: true,
						localIdentName: '[name]__[local]--[hash:base64:5]'
					}
				}, {
					loader: 'postcss-loader'
				}]
			}, {
				test: /\.less$/,
				include: [
					path.resolve(__dirname, "websrc/main"),
					path.resolve(__dirname, "websrc/test")
				],
				use: [{
					loader: 'style-loader' // creates style nodes from JS strings
				}, {
					loader: 'css-loader', // translates CSS into CommonJS
					options: {
						importLoaders: 1,
						modules: true,
						localIdentName: '[name]__[local]--[hash:base64:5]'
					}
				}, {
					loader: 'postcss-loader'
				}, {
					loader: 'less-loader' // compiles Less to CSS
				}]
			}, {
				test: /\.css$/,
				include: [
					path.resolve(__dirname, "node_modules")
				],
				use: [{
					loader: 'style-loader'
				}, {
					loader: 'css-loader',
					// 为了 Ant-Design 得把下面注释掉
					options: {
						importLoaders: 1,
						// modules: true,
						// localIdentName: '[name]__[local]--[hash:base64:5]'
					}
				}, {
					loader: 'postcss-loader'
				}]
			}, {
				test: /\.less$/,
				include: [
					path.resolve(__dirname, "node_modules")
				],
				use: [{
					loader: 'style-loader' // creates style nodes from JS strings
				}, {
					loader: 'css-loader', // translates CSS into CommonJS
					// 为了 Ant-Design 得把下面注释掉
					options: {
						importLoaders: 1,
						// modules: true,
						// localIdentName: '[name]__[local]--[hash:base64:5]'
					}
				}, {
					loader: 'postcss-loader'
				}, {
					loader: 'less-loader' // compiles Less to CSS
				}]
			}, {
                test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
                include: [
                    path.resolve(__dirname, "websrc/main"),
                    path.resolve(__dirname, "websrc/test")
                ],
				use: [{
					loader: 'url-loader',
					options: {
						module: true,
						limit: 10240
					}
				}]
			}]
	},

	plugins: [
		// html 模板插件
		new HtmlWebpackPlugin({
			template: __dirname + '/websrc/main/index.tmpl.html'
		}),

		// 热加载插件
		new webpack.HotModuleReplacementPlugin(),

		// 打开浏览器
		new OpenBrowserPlugin({
		    browser: 'google-chrome-stable',
			url: 'http://localhost:8080'
		}),

		// 可在业务 js 代码中使用 __DEV__ 判断是否是dev模式（dev模式下可以提示错误、测试报告等, production模式不提示）
		new webpack.DefinePlugin({
			__DEV__: JSON.stringify(JSON.parse((process.env.NODE_ENV == 'dev') || 'false'))
		})
	],

	devServer: {
		// contentBase: resolve(__dirname, 'dist'),
		// contentBase: __dirname + '/webbuild',
		// 输出文件的路径
		historyApiFallback: true, //不跳转，在开发单页应用时非常有用，它依赖于HTML5 history API，如果设置为true，所有的跳转将指向index.html
		inline: true, //实时刷新
		hot: true,  // 使用热加载插件 HotModuleReplacementPlugin
		proxy: {
			'/data/cityinfo/*': {
				target: 'http://www.weather.com.cn',
				changeOrigin: true,
				secure: false
			},
			'/api/**': {
				target: 'http://localhost:8888/demo',
				changeOrigin: true,
				secure: false
			}
		}
	}

};
			